//
//  EJSUserModel.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/29.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSUserModel.h"
#import "EJSUserInfoRequest.h"
#import <JPUSHService.h>
#import "EJSShoppingCarSingleton.h"

#define UserTokenModel @"UserTokenModel"
#define UserModel @"UserModel"

@implementation EJSUserModel

- (void)save{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[self getJsonDictionary] forKey:UserModel];
    [userDefaults synchronize];
}

+ (EJSUserModel *)userModel{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults synchronize];
    NSDictionary *dict = [userDefaults objectForKey:UserModel];
    if(dict){
        EJSUserModel *userModel = [EJSUserModel objectWithDictionaryRepresentation:dict];
        return userModel;
    }
    return nil;
}

+ (void)logout{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:UserModel];
    [userDefaults removeObjectForKey:UserTokenModel];
    [userDefaults synchronize];
    [JPUSHService setAlias:nil completion:nil seq:0];
    [[EJSShoppingCarSingleton sharedSingleton]logoutAndEmptyShoppingCar];
}

+ (void)syncUserInfo{
    EJSUserInfoRequest *req = [EJSUserInfoRequest new];
    req.hideLoadingView = YES;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
    } failHandler:^(BaseResponse *response) {
    }];
}

+ (void)saveCountryCode:(NSString *)countryCode{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:countryCode forKey:DefaultCountryCode];
    [userDefaults synchronize];
}
+ (NSString *)countryCode{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults synchronize];
    if([userDefaults objectForKey:DefaultCountryCode]){
        return [userDefaults objectForKey:DefaultCountryCode];
    }
    return @"+34";
}
@end


@implementation EJSUserTokenModel

- (void)save{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[self getJsonDictionary] forKey:UserTokenModel];
    [userDefaults synchronize];
    
    [JPUSHService setAlias:self.token completion:nil seq:0];
}

+ (EJSUserTokenModel *)userTokenModel{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults synchronize];
    NSDictionary *dict = [userDefaults objectForKey:UserTokenModel];
    if(dict){
        EJSUserTokenModel *userTokenModel = [EJSUserTokenModel objectWithDictionaryRepresentation:dict];
        return userTokenModel;
    }
    return nil;
}

@end

